<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改收款记录')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="main-content">
        <form class="form-horizontal m" id="form-detail-add" th:object="${receivableAccount}">
            <input type="hidden" name="id" th:value="*{id}">
            <div class="row">
                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">项目/结算名称：</label>
                    <div class="col-sm-8">
                        <input name="projectName"  class="form-control" type="text" th:value="*{title}" disabled/>
                        <input name="planId" class="form-control" type="hidden" th:value="*{planId}"/>
                        <input name="projectId" class="form-control" type="hidden" th:value="*{projectId}"/>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">收款笔数：</label>
                    <div class="col-sm-8">
                        <select name="planNumber" class="form-control m-b" th:field="*{plan.planNumber}" th:with="type=${@dict.getType('account_number')}" disabled>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{plan.planNumber}"></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">计划收款金额：</label>
                    <div class="col-sm-8">
                        <input name="expectAmount" th:field="*{plan.expectAmount}" type="text" class="form-control" disabled>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-3 control-label is-required">收款金额：</label>
                    <div class="col-sm-8">
                        <input name="receiveAmount" id="receiveAmount" class="form-control m-b" type="text" th:field="*{receiveAmount}" readonly>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">收款时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="receiveTime" class="form-control" placeholder="yyyy-MM-dd" type="text" th:value="${#dates.format(receivableAccount.receiveTime, 'yyyy-MM-dd')}">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>

                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">收款账户：</label>
                    <div class="col-sm-8">
                        <select name="bankId"  class="form-control m-b"  th:with="type=${@customDictService.selectDictBanks()}">
                            <option value="0">无</option>
                            <option th:each="dict : ${type}" th:text="${dict.name}" th:value="${dict.id}" th:selected="${receivableAccount.bankId == dict.id}" ></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="row">

                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">收款人：</label>
                    <div class="col-sm-8">
                        <select name="receiverId"  class="form-control m-b"  th:with="type=${@customDictService.selectUserList()}">
                            <option value="0">无</option>
                            <option th:each="dict : ${type}" th:text="${dict.userName}" th:value="${dict.userId}" th:selected="${receivableAccount.receiverId == dict.userId}" ></option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <input type="text" th:field="*{description}" name="description" class="form-control" />
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">结算账单</h4>
            <div class="row">
                <div class="col-sm-12">
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "erp/receivable/detail";
        var settleReceivables = [[${receivableAccount.getSettleReceivable()}]];
        $("#form-detail-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-detail-add').serialize());
            }
        }

        $("input[name='receiveTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            // 初始化数据, 可以由后台传过来
            var options = {
                data: settleReceivables,
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [
                    {
                        field: 'settleDetailId',
                        title: '',
                        visible: false
                    },
                    {
                        field: 'id',
                        title: '',
                        visible: false
                    },
                    {
                        field: 'customerId',
                        title: '',
                        visible: false
                    },
                    {
                        field: 'projectName',
                        title: '项目名称'
                    },
                    {
                        field: 'personName',
                        width: '60px',
                        title: '姓名'
                    },
                    {
                        field: 'year',
                        title: '年-月',
                        formatter(value, row, index)
                        {
                            return value + "年" + row.month + "月";
                        }
                    },
                    {
                        field: 'month',
                        title: '月'
                    },
                    {
                        field: 'expireTime',
                        title: '收款截止期'
                    },
                    {
                        field: 'billedAmount',
                        title: '账单费用'
                    },
                    {
                        field: 'amount',
                        title: '收款金额',
                        width: '200px',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control amount' type='text' name='settleReceivable[%s].amount' value='%s'>", index, value);
                            html = html + $.common.sprintf("<input class='form-control' type='hidden' name='settleReceivable[%s].receivablePlanId' value='%s'>", index, row.receivablePlanId);
                            html = html + $.common.sprintf("<input class='form-control' type='hidden' name='settleReceivable[%s].settleDetailId' value='%s'>", index, row.settleDetailId);
                            html = html + $.common.sprintf("<input class='form-control' type='hidden' name='settleReceivable[%s].settleId' value='%s'>", index, row.settleId);
                            html = html + $.common.sprintf("<input class='form-control' type='hidden' name='settleReceivable[%s].id' value='%s'>", index, row.id);
                            return html;
                        }
                    }
                ]
            };
            $.table.init(options);
        });

        $(function(){
            $(document).on("blur",'.amount',function(){
                calculateTotal();
            });
        });

        function calculateTotal(){
            var total = 0;
            $('.amount').each(function (){
                if(!isNaN(parseFloat($(this).val()))){
                    total = total + parseFloat($(this).val());
                }

            });
            $('#receiveAmount').val(total);
        }

    </script>
</body>
</html>